package com.wuliqing.android.zte.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wuliqing.android.zte.data.CallLogData;
import com.wuliqing.android.zte.data.ContactData;
import com.wuliqing.android.zte.drawable.RoundImageDrawable;
import com.wuliqing.android.zte.myapplication.R;

import java.util.List;

public class RecyclerViewCallLogAdapter extends RecyclerView.Adapter<RecyclerViewCallLogAdapter.ViewHolder> {

    private Context mContext;
    private List<CallLogData> mContactDataList;
    public RecyclerViewCallLogAdapter(Context mContext, List<CallLogData> list) {
        this.mContext = mContext;
        mContactDataList = list;
    }

    @Override
    public RecyclerViewCallLogAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =
                LayoutInflater.from(parent.getContext()).inflate(R.layout.calllog_list_item_card, parent, false);
        return new ViewHolder(view);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(final RecyclerViewCallLogAdapter.ViewHolder holder, int position) {
        final View view = holder.mView;
        holder.position = position;
        CallLogData contactData = mContactDataList.get(position);
        holder.name_v.setText(contactData.getName());
        if (contactData.getTime() != 0){
            CharSequence time_string = DateUtils.getRelativeTimeSpanString(contactData.getTime(),
                    System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS,
                    DateUtils.FORMAT_ABBREV_RELATIVE);
            holder.time_v.setText(time_string);
        }else {
            holder.time_v.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return mContactDataList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public final View mView;
        public int position;
        public TextView name_v;
        public TextView number_v;
        public TextView time_v;
        public ViewHolder(View view) {
            super(view);
            mView = view;
            mView.setOnClickListener(this);
            name_v = (TextView)view.findViewById(R.id.calllog_name);
            number_v = (TextView)view.findViewById(R.id.calllog_number);
            time_v = (TextView)view.findViewById(R.id.calllog_time);
        }

        @Override
        public void onClick(View v) {
            if (null != onRecyclerViewListener) {
                onRecyclerViewListener.onItemClick(position);
            }
        }

    }

    public interface OnRecyclerViewListener {
        void onItemClick(int position);
    }

    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }
}
